package com.itmatter.classifier.rmi.server;

import com.itmatter.classifier.rmi.RemoteClassifierInterface;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import org.apache.log4j.Logger;

/**
 * 
 *
 */
public class App {
    
    private static final Logger logger = Logger.getLogger(RemoteClassifierImpl.class.getName());
    
    public static void main(String[] args) {
        // Create and install a security manager
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
            System.out.println("Security manager installed.");
        } else {
            System.out.println("Security manager already exists.");
        }
        
        Registry registry = null;
        int port = 1099;
        try { //special exception handler for registry creation
            registry = LocateRegistry.createRegistry(port);
            System.out.println("Test Server =>: Registry on CUSTOM port (" + port + ") created.");
        } catch (RemoteException e) {
            //do nothing, error means registry already exists
            System.out.println("java RMI registry already exists.");
        }
        
        try {
            //Instantiate RmiServer
            RemoteClassifierInterface classifier = new RemoteClassifierImpl();
            
            String name = "//localhost/RemoteClassifierImpl";

            // Bind this object instance to the name
            Naming.rebind(name, classifier);
            
            System.out.println("Remote Classifier bound in registry");
        } catch (Exception e) {
            System.err.println("RMI server exception:" + e);
            logger.error(e.getMessage(), e);
        }
    }
}
